package com.yimils.core.sys.controller;


import com.yimils.core.common.dto.RspBase;
import com.yimils.core.common.dto.SysCode;
import com.yimils.core.sys.beanUtils.UserResourceUtil;
import com.yimils.core.sys.dto.ReqResource;
import com.yimils.core.sys.dto.ResourceDto;
import com.yimils.core.sys.dto.RoleResourceDto;
import com.yimils.core.sys.entity.ResourceEntity;
import com.yimils.core.sys.service.ResourceService;
import com.yimils.core.sys.service.RoleResourceService;
import com.yimils.core.sys.vo.UserMenuVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class ResController {
    @Resource(name = "roleResourceService")
    private RoleResourceService roleResourceService;

    @Resource(name = "resourceService")
    private ResourceService resourceService;

    @Autowired
    private ResourceDto resourceDto;

    @Autowired
    private RoleResourceDto roleResourceDto;

    @Autowired
    private UserResourceUtil userResourceUtil;

    /**
     * showdoc
     *
     * @param roleId 必选 PathVariable 角色id
     * @return {"code":0,"msg":"","data":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]}
     * @catalog 系统管理/权限管理
     * @title 获取角色权限
     * @description 获取系统中角色所拥有的权限
     * @method get
     * @url /res
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @GetMapping("/res")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase getRoleResources(@RequestParam Long roleId) {

        //List<RoleResourceEntity> roleRes = roleResourceService.getRoleResourcesTree(roleId);
        List<ResourceEntity> resources = resourceService.getPermByRoleId(null, roleId);
        RspBase rsp = RspBase.OK();
        rsp.put("data", userResourceUtil.entitiesToArray(resources));
        return rsp;
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"修改成功"}
     * @catalog 系统管理/权限管理
     * @title 修改角色权限
     * @description 修改角色所拥有的权限，参数中未包含的id将会设置为关闭
     * @json_param {"roleId":3,"roleRes":[1,2,3,4,5,7,8,9,10,11,12,13]}
     * @method put
     * @url /res
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @PutMapping("/res")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase updateRoleResource(@RequestBody ReqResource reqParams) {
        if (roleResourceService.updateRoleResourceForBatch(reqParams.getRoleId(), roleResourceDto.arrayToEntities(reqParams))) {
            return RspBase.OK("修改成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "修改失败");
    }

    /**
     * showdoc
     *
     * @return {"code":200,"msg":"","data":[{"id":1,"parentId":null,"name":"sys","label":"系统管理","icon":"fa fa-cogs","opt":null,"url":null,"sort":1,"sys":1,"children":[{"id":2,"parentId":1,"name":"user","label":"用户管理-查看","icon":null,"opt":"view","url":"/sys/user","sort":1,"sys":1,"children":[{"id":3,"parentId":1,"name":"user","label":"用户管理-添加","icon":"fa fa-cog","opt":"add","url":"/sys/user","sort":1,"sys":1},{"id":4,"parentId":1,"name":"user","label":"用户管理-修改","icon":"fa fa-cog","opt":"update","url":"/sys/user","sort":1,"sys":1},{"id":5,"parentId":1,"name":"user","label":"用户管理-删除","icon":null,"opt":"remove","url":null,"sort":1,"sys":1}]},{"id":7,"parentId":1,"name":"role","label":"角色管理-查看","icon":null,"opt":"view","url":"/sys/role","sort":2,"sys":1,"children":[{"id":8,"parentId":1,"name":"role","label":"角色管理-添加","icon":"fa fa-cog","opt":"add","url":"/sys/role","sort":1,"sys":1},{"id":9,"parentId":1,"name":"role","label":"角色管理-修改","icon":"fa fa-cog","opt":"update","url":"/sys/role","sort":1,"sys":1},{"id":10,"parentId":1,"name":"role","label":"角色管理-删除","icon":"fa fa-cog","opt":"remove","url":"/sys/role","sort":1,"sys":1}]},{"id":6,"parentId":1,"name":"perm","label":"权限管理-查看","icon":null,"opt":"view","url":"/sys/perm","sort":3,"sys":1,"children":[{"id":11,"parentId":1,"name":"perm","label":"权限管理-添加","icon":null,"opt":"add","url":"/sys/perm","sort":1,"sys":1},{"id":12,"parentId":1,"name":"perm","label":"权限管理-修改","icon":null,"opt":"update","url":"/sys/perm","sort":1,"sys":1},{"id":13,"parentId":1,"name":"perm","label":"权限管理-删除","icon":null,"opt":"remove","url":"/sys/perm","sort":1,"sys":1}]},{"id":20,"parentId":1,"name":"resTree","label":"菜单管理-查看","icon":"","opt":"view","url":"/sys/resTree","sort":4,"sys":1,"children":[{"id":21,"parentId":1,"name":"resTree","label":"菜单管理-添加","icon":"","opt":"add","url":"/sys/resTree","sort":1,"sys":1},{"id":22,"parentId":1,"name":"resTree","label":"菜单管理-修改","icon":"","opt":"update","url":"/sys/resTree","sort":1,"sys":1},{"id":23,"parentId":1,"name":"resTree","label":"菜单管理-删除","icon":"","opt":"remove","url":"/sys/resTree","sort":1,"sys":1}]}]}]}
     * @catalog 系统管理/菜单管理
     * @title 获取菜单
     * @description 获取系统中菜单树结构
     * @method get
     * @url /resTree
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @GetMapping("/resTree")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase getResourcesTree() {
        List<UserMenuVo.Menu> resTree = userResourceUtil.getMenuTree(null, true);
        RspBase rsp = RspBase.OK();
        rsp.put("data", resTree);
        return rsp;
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"创建成功"}
     * @catalog 系统管理/菜单管理
     * @title 添加菜单
     * @description 添加系统中权限菜单
     * @json_param {"parentId":null,"name":"sys","menuName":"系统管理","icon":"xxxx","opt":null,"url":null,"sort":1, "sys":1}
     * @method post
     * @url /resTree
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限, 注意创建菜单目录和菜单项参数的不同
     * @number 3
     */
    @PostMapping("/resTree")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase createResource(@RequestBody ResourceEntity reqParams) {
        if (resourceService.create(reqParams)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "操作失败");
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"修改成功"}
     * @catalog 系统管理/菜单管理
     * @title 修改菜单
     * @description 修改系统中权限菜单
     * @json_param [{"id":1,"parentId":null,"name":"sys","menuName":"xxx","icon":"xxxx","opt":null,"url":null,"sort":1,"sys":1},{"id":2,"parentId":null,"name":"perm","menuName":"xxx","icon":"xxxx","opt":null,"url":null,"sort":1,"sys":1}]
     * @method put
     * @url /resTree
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限, 参数为列表形式
     * @number 3
     */
    @PutMapping("/resTree")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase updateResource(@RequestBody List<ResourceEntity> reqParams) {
        if (resourceService.update(reqParams)) {
            return RspBase.OK("修改成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "修改失败");
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"删除成功"}
     * @catalog 系统管理/菜单管理
     * @title 删除菜单
     * @description 删除系统中权限菜单
     * @method delete
     * @url /resTree/{id}
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限, 参数为列表形式
     * @number 3
     */
    @DeleteMapping("/resTree/{id}")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase deleteResource(@PathVariable Long id) {
        if (resourceService.delete(id)) {
            return RspBase.OK("删除成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "删除成功");
    }

    /**
     * showdoc
     *
     * @return {"code":200,"msg":"","data":[{"id":1,"parentId":null,"name":"sys","menuName":"系统管理","icon":"fa fa-cogs","opt":null,"url":null,"sort":1,"sys":1},{"id":28,"parentId":null,"name":"test-sys","menuName":"测试系统菜单创建","icon":"fa fa-bank","opt":null,"url":null,"sort":2,"sys":1},{"id":29,"parentId":null,"name":"test-not-sys","menuName":"测试非系统菜单目录","icon":"fa fa-cutlery","opt":null,"url":null,"sort":3,"sys":0}]}
     * @catalog 系统管理/菜单管理
     * @title 获取菜单目录
     * @description 获取系统中所有菜单目录项
     * @method get
     * @url /dirs
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限, 参数为列表形式
     * @number 3
     */
    @GetMapping("/dirs")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase getDirList() {
        RspBase rsp = RspBase.OK();
        rsp.put("data", resourceService.getDirs());
        return rsp;
    }
}
